<?php
/* bossWebSearch - interface for use with Yahoo search BOSS API web searches*/
     require_once("yahooConfig.php");

class bossWebSearch
{
        protected $url;            // Interface(URL) used to gain the data
        protected $xml;            // Raw XML
        protected $xmlObj;         // SimpleXML object
	protected $searchTerms;    //

     public function __construct($terms = NULL)
     {
	  $this->searchTerms = $terms;
     }

     public function __get($name)
     {
	  return $this->$name;
     }

     public function __set($attribute, $value)
     {
	  $this->$attribute = $value;
     }

     public function addTerm($term)
     {
	  $this->searchTerms .= $term."%20";
     }

     /* returns the number of hits for this search */
     public function getHits()
     {
	  if(empty($this->xmlObj->resultset_web->result[0])){
		    throw new NoWebResultsException();
	  }
	  return $this->xmlObj->resultset_web['totalhits'];

     }

     /*getKeywords -                                      */
     /* performs a Yahoo web search for the searchTerms   */
     /* returns an array of strings consisting of the     */
     /* keyterms and Delicious tags (if there are any)    */
     /* returned by all of the 50 results                 */

     public function getKeywords($stopArray = array())
     {

	  if(empty($this->xmlObj->resultset_web->result[0])){
		    throw new NoWebResultsException();
	  }
	  //otherwise good to go
	  $keywords = array();
	  foreach($this->xmlObj->resultset_web->result as $res)
	  {
	       foreach($res->keyterms->terms->term as $keywd)
	       {

	       	  //format the returned words for cloud
		   //remove all numbers and punctuation
		    $keywd = (string)$keywd;
		    $keywd = preg_replace('/[^a-zA-Z]/',' ', $keywd);
		    $keywd = strtolower($keywd);
		    //put individual words into an array
		    $results = explode(" ", $keywd);
		    //remove all whitespaces
		    //for some reason cannot save this back to same array
		    $fixedResults = array_filter($results,'trim');
		    //take out the stop words before putting into cloud
		    $fixedResults = array_diff($fixedResults, $stopArray);
		    foreach($fixedResults as $kw)
		    {
			 $keywords[] = $kw;
		    }
	       }
	       if(count($res->delicious_toptags->tags) > 0){
		    foreach($res->delicious_toptags->tags->tag as $tag)
		    {

		    //format the returned words for cloud
		    //remove all numbers and punctuation
			 $tag = (string)$tag;
			 $tag = preg_replace('/[^a-zA-Z]/',' ', $tag);
			 $tag= strtolower($tag);
			 //put individual words into an array
			 $results = explode(" ", $tag);
			 //remove all whitespaces
			 //for some reason cannot save this back to same array
			 $fixedResults = array_filter($results,'trim');
			 //take out the stop words before putting into cloud
			 $fixedResults = array_diff($fixedResults, $stopArray);
			 foreach($fixedResults as $t)
			 {
			      $keywords[] = $t;
			 }
		    }
	       }
	  }
	  return $keywords;
     }

     public function search()
     {
	  if(empty($this->searchTerms)){
	       throw new NeedSearchTermsException();
	  }

	  if (!$this->xml = file_get_contents(BOSSWEBURL . $this->searchTerms . BOSSWEBARGS)) {
	       throw new NoWebResultsException();
	  }
	  //all is good make simple XML object
	  $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA);

     }


}//end bossWebSearch class



class NeedSearchTermsException extends Exception
{
     function _toString()
     {
	  return 'NeedSearchTermsException '. $this->getCode(). ': '. $this->getMessage().'<br /> in '. $this->getFile(). ' on line '.$this->getLine(). '<br />';
     }
}

class NoWebResultsException extends Exception
{
     function _toString()
     {
	  return 'NoWebResultsException'. $this->getCode(). ': '. $this->getMessage().'<br />'.' in '. $this->getFile(). ' on line '.$this->getLine(). '<br />';
     }
}


?>